#!/bin/bash
##############################################################
source ./pkgutils.sh || exit 1
PKG_NAME=$(pkgname); if (( $? != 0)); then exit 2; fi
PKG_VERSION=$(pkgversion); if (( $? != 0)); then exit 3; fi
if [[ $(whoami) != "root" ]]; then
	echo "Please log in as user root...";
	exit 4;
fi
mkdir -pv ./tmp && cd ./tmp || exit 5;
echo "setup of $PKG_NAME $PKG_VERSION";
##############################################################
# do setup tasks (location: /usr/pkg/$PKG_NAME/$PKG_VERSION/tmp)
chown lfs:lfs ../tmp && \
su -p -c "PATH=$PATH; cd /usr/pkg/$PKG_NAME/$PKG_VERSION/tmp && \
rm -f certdata.txt && \
wget \"http://anduin.linuxfromscratch.org/sources/other/certdata.txt\"  || \
{ 	echo 'Failed to download CA certificates... use CA certificates of package archive'; \
	cp -f ../certdata.txt ./; }  && \
make-ca.sh         && \
remove-expired-certs.sh certs" lfs && \
SSLDIR=/etc/ssl                                              && \
install -d ${SSLDIR}/certs                                   && \
echo "/etc/ssl/certs" >> /usr/pkg/installed.db/${PKG_NAME}_${PKG_VERSION}_pkg.files && \
cp -v certs/*.pem ${SSLDIR}/certs                            && \
find ${SSLDIR}/certs/ -name '*.pem' >> /usr/pkg/installed.db/${PKG_NAME}_${PKG_VERSION}_pkg.files && \
c_rehash                                                     && \
install BLFS-ca-bundle*.crt ${SSLDIR}/ca-bundle.crt          && \
echo "${SSLDIR}/ca-bundle.crt" >> /usr/pkg/installed.db/${PKG_NAME}_${PKG_VERSION}_pkg.files && \
ln -sf ../ca-bundle.crt ${SSLDIR}/certs/ca-certificates.crt && \
echo "${SSLDIR}/certs/ca-certificates.crt" >> /usr/pkg/installed.db/${PKG_NAME}_${PKG_VERSION}_pkg.files

# hint: write all file modifications/copy/mv/ln into: /usr/pkg/installed.db/${PKG_NAME}_${PKG_VERSION}_pkg.files
##############################################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package setup with code $ERR";
    exit 6;
fi
